package cn.pconline.autoclub.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.pconline.autoclub.entity.Subject;
import cn.pconline.autoclub.entity.User;
import cn.pconline.autoclub.service.UserSubjectService;
import cn.pconline.autoclub.util.AppUtils;
import cn.pconline.autoclub.util.EncodeUtils;
import cn.pconline.autoclub.util.Pager;
import cn.pconline.autoclub.util.WebUtils;

/**
 * 用户话题
 * @author fgs
 *
 */
@Controller
@RequestMapping("/userSubject")
public class UserSubjectController {

	public static final int MYSUBJECT_PAGE_SIZE = 0;//正常
	
    @Autowired
    private UserAuth userAuth;
    
    @Autowired
    private UserSubjectService userSubjectService;
    
    /**
     * 加载更多我的话题
     * @param request
     * @param response
     */
	@ResponseBody
    @RequestMapping(value = "/loadMoreMySubject.do", method = RequestMethod.POST)
	public void loadMoreMySubject(HttpServletRequest request, HttpServletResponse response){
		WebUtils.setEncoding(request, response);
		String callback = WebUtils.param(request, "callback", "");
		int pageNo = WebUtils.paramInt( request, "pageNo", 1 );
		int pageSize = WebUtils.paramInt( request, "pageSize", MYSUBJECT_PAGE_SIZE );
		User user = userAuth.getUser(request, response);//验证用户是否登录
		if(user==null){
			AppUtils.printErrorMsg(response,"请先登录", callback);
        	return;
		}
		JSONObject jsonObject = new JSONObject();
		Pager<Subject> pager = userSubjectService.pageByUserIdByHot(user.getUid(),pageNo,pageSize);
		if(pager!=null){
			int pageCount = pager.getPageCount();
			jsonObject.put("pageCount", pageCount );
			List<Subject> list = pager.getResultList();
			JSONArray jsonArray = new JSONArray();
			if(list!=null){
				for(Subject subject:list) {
					JSONObject tempObject = new JSONObject();
	                tempObject.put("id",  subject.getSubjectId() );
	                tempObject.put("title",EncodeUtils.encodeForHTMLFilterNull(subject.getTitle()));
	                long pvCount = subject.getPvCount();
	                tempObject.put("pvCount", pvCount > 10000 ? (pvCount/100+5)/ 10 / 10.0 + "万" : pvCount );
	                jsonArray.add( tempObject);
                }
			}
			jsonObject.put("subjects", jsonArray );
		}
		AppUtils.printSuccessMsg( response, "操作成功", jsonObject, callback );
		
		
	}
}
